<?php

class Questionary
{
	private $questionary;
	private $questions_total;
	private $questions_right;
	private $questions_percentage;
	private $corrected;
	
	function __construct()
	{
		$conn = mysql_pconnect('localhost', 'root', '') or die ('Error connecting to mysql');
		mysql_select_db('youknow');
	}

	
	function __destruct()
	{
//		print "Destroying " . $this->name . "\n";
//		echo "CLASS DIED!!!";
	}


	function clean()
	{
		$this->questionary = array();
		$this->questions_total = 0;
		$this->questions_right = 0;
		$this->questions_percentage = 0;
		$this->corrected = false;
	}
	
	
	function correct()
	{
		$this->questions_right = 0;
		
		foreach ($this->questionary as $question_index => $question_value)
		{
			$question_correct = 'Y';
			
			foreach ($question_value['answer'] as $answer_index => $answer_value)
			{
				if (($answer_value['selected'] == 'Y') && ($answer_value['correct'] == 'Y'))
				{
					$answer_label = 'right'; 
				}
				elseif (($answer_value['selected'] == 'Y') && ($answer_value['correct'] == 'N'))
				{
					$answer_label = 'wrong';
					$question_correct = 'N';
				}
				elseif (($answer_value['selected'] == 'N') && ($answer_value['correct'] == 'Y'))
				{
					$answer_label = 'missed';
					$question_correct = 'N';
				}
				elseif (($answer_value['selected'] == 'N') && ($answer_value['correct'] == 'N'))
				{
					$answer_label = 'notmarked';
				}
				else
				{
					$answer_label = 'error';
					$question_correct = 'N';
				}
				$this->questionary[$question_index]['answer'][$answer_index]['label'] = $answer_label;
			}
			$this->questionary[$question_index]['correct'] = $question_correct;
			
			if ($question_correct == 'Y') $this->questions_right++;
		}
		$this->questions_percentage = round(($this->questions_right/$this->questions_total)*100);
		$this->corrected = true;
	}
	
	
	function getPercentage()
	{
		return $this->questions_percentage;
	}
	
	
	function getResult()
	{
		$result = array();
		
		foreach ($this->questionary as $question_id => $question_value)
		{
			$result[$question_id] = $question_value['correct'];
		}
		return $result;
	}
	
	
	function getTotal()
	{
		return $this->questions_total;
	}


	function getTotalRight()
	{
		return $this->questions_right;
	}
	
	
	function load($size, $random = true)
	{
		$this->clean();
		
		$query_question = mysql_query("SELECT * FROM question".(($random)? ' ORDER BY RAND()' : '')." LIMIT ".$size);
		$this->questions_total = mysql_num_rows($query_question);

		while ($fetch_question = mysql_fetch_assoc($query_question))
		{
			$this->questionary[$fetch_question['id']]['text'] = $fetch_question['text'];
			$this->questionary[$fetch_question['id']]['explanation'] = $fetch_question['explanation'];

			$query_tag = mysql_query('SELECT t.text FROM tag t, question q, question_tag qt WHERE t.id = qt.tag_id AND q.id = qt.question_id AND q.id = '.$fetch_question['id']);

			while ($tag = mysql_fetch_assoc($query_tag))
			{
				$this->questionary[$fetch_question['id']]['tags'] .= $tag['text'].' ';
			}
			$this->questionary[$fetch_question['id']]['tags'] = trim($this->questionary[$fetch_question['id']]['tags']);

			$questionary_answer = array();
			$answers_correct = 0;

			$query_answer = mysql_query('SELECT * FROM answer WHERE question_id = '.$fetch_question['id'].' ORDER BY RAND()');
			while ($fetch_answer = mysql_fetch_assoc($query_answer))
			{
				$this->questionary[$fetch_question['id']]['answer'][$fetch_answer['id']]['text'] = $fetch_answer['text'];
				$this->questionary[$fetch_question['id']]['answer'][$fetch_answer['id']]['explanation'] = $fetch_answer['explanation'];
				$this->questionary[$fetch_question['id']]['answer'][$fetch_answer['id']]['correct'] = $fetch_answer['correct'];
				$this->questionary[$fetch_question['id']]['answer'][$fetch_answer['id']]['label'] = 'unknow';
				$this->questionary[$fetch_question['id']]['answer'][$fetch_answer['id']]['selected'] = 'N';
				if ($fetch_answer['correct'] == 'Y') $answers_correct++;
			}
			$this->questionary[$fetch_question['id']]['answers_correct'] = $answers_correct;
		}
	}
	
	
	function setAnswer($question, $answer)
	{
		$this->questionary[$question]['answer'][$answer]['selected'] = 'Y';
	}

	
	function printHTML($identation = 0, $showbuttons = true)
	{
		$tab = "";
		for ($i = 0; $i < $identation; $i++) $tab .= "\t";
			
		if (!$this->corrected)
		{
			echo $tab.'<form id="questionary" name="questionary" method="post" action="'.FILENAME.'">'."\n\n";
		}
		else
		{
			echo $tab.'<div id="questionary">'."\n\n";
		}
		
		foreach ($this->questionary as $question_index => $question_value)
		{
			echo $tab."\t".'<fieldset class="item">'."\n";
			echo $tab."\t\t".'<legend class="title">OBJECTIVE: 2.11.4 TYPE: mc</legend>'."\n";
			if ($this->corrected) echo $tab."\t\t".'<img class="icon" src="images/'.(($question_value['correct'] == "Y")? 'right' : 'wrong').'.png" alt="" />'."\n";
			
			echo $tab."\t\t".'<div class="question">'.$question_value['text'].'</div>'."\n";
			echo $tab."\t\t".'<ol class="answers">'."\n";

			$answernum = 0;
			foreach ($question_value['answer'] as $answer_index => $answer_value)
			{
				$multiple = ($question_value['answers_correct'] > 1)? true : false;
				echo $tab."\t\t\t".'<li class="'.$answer_value['label'].'">'."\n";
				if (!$this->corrected) echo $tab."\t\t\t\t".'<input type="'.(($multiple)? 'checkbox' : 'radio').'" name="question_'.$question_index.(($multiple)? '_'.($answernum++) : '').'" value="'.$answer_index.'" />'."\n";
				echo $tab."\t\t\t\t".$answer_value['text']."\n";
				if (($this->corrected) && ($answer_value['explanation'])) echo "\t\t\t\t\t\t".'<div class="explanation">'.$answer_value['explanation'].'</div>'."\n"; 
			}
			echo $tab."\t\t\t".'</li>'."\n";

			echo $tab."\t\t".'</ol>'."\n";
			echo $tab."\t\t".'<div class="tags">'.$question_value['tags'].'</div>'."\n";
			echo $tab."\t".'</fieldset>'."\n\n";
		}
		
		if ((!$this->corrected) && ($showbuttons))
		{
			echo $tab."\t".'<div class="submit">'."\n";
			echo $tab."\t\t".'<input type="submit" class="button" value="Send" />'."\n";
			echo $tab."\t".'</div>'."\n\n";
		}

		if (!$this->corrected)
		{
			echo $tab."\t".'<input type="hidden" name="questionary" value="1" />'."\n";
			echo $tab.'</form>'."\n";
		}
		else
		{
			echo $tab.'</div>'."\n";
		}
	}
}

?>